<?php
	class MailAction extends CommonAction{
   
		public $page; 			// 当前页数
		public $listRows = 6;	// 每页显示行数
		public $rollPage = 5;	// 每页栏导航显示页数的个数
		
		public function index(){
			$this->display();
		}
		
		//发信箱
		public function sendmail(){
			$countres=M()->query('select count(Id) num from mail where Sender='.$_SESSION[C('USER_AUTH_KEY')]['UserId'].' and IsDeleted=0 ');
			$count=$countres[0]['num'];// 总记录条数 
		  	if(!$page=intval($this->_get('page'))){
				$page=1;
			}
			$totalPages=ceil($count/$this->listRows);
			$page=($page<=1)?1:(($page>ceil($count/$this->listRows))?ceil($count/$this->listRows):$page);
			$start=($page-1)*($this->listRows);
			$mail = M()->query('select Id ,Title,Content,CreateDate from mail where Sender='.$_SESSION[C('USER_AUTH_KEY')]['UserId'].' and IsDeleted=0 order by CreateDate desc limit '.$start.' , '.$this->listRows);
			$this->assign('page',$page); //当前页
			$this->assign('totalPages',$totalPages); //总页数
			$this->assign('sendmail',$mail);
			$this->display();
		}
		
		//收信箱
		public function receivmail(){
			$countres=M()->query('select count(Id) num from mail where Receiverid='.$_SESSION[C('USER_AUTH_KEY')]['UserId'].' and IsDeleted=0 ');
			$count=$countres[0]['num'];// 总记录条数 
		  	if(!$page=intval($this->_get('page'))){$page=1;}
			$totalPages=ceil($count/$this->listRows);
			$page=($page<=1)?1:(($page>ceil($count/$this->listRows))?ceil($count/$this->listRows):$page);
			$start=($page-1)*($this->listRows);
			$mail = M()->query('
				select m.Id ,m.Title,m.Sender,m.SenderName,m.CreateDate,m.IsRead 
				from mail m
				where Receiverid='.$_SESSION[C('USER_AUTH_KEY')]['UserId'].' 
				and IsDeleted=0 
				order by m.IsRead asc, CreateDate desc
				limit '.$start.' , '.$this->listRows
			);
			$i=0;
			foreach($mail as $ml){
				if($ml['Sender']==0)
					$mail[$i]['UserType']=0;
				else
					$mail[$i]['UserType'] = $this->getutype($ml['Sender']);
				$i++;
			}
			$this->assign('page',$page); //当前页
			$this->assign('totalPages',$totalPages); //总页数
			$this->assign('list',$mail);
			$this->display();
		}
		
		//查询用户的类别
		public function getutype($id){
			$type = M()->query('select UserType from users where UserId ='.$id);
			return $type[0]['UserType'];
		}
		
		//删除邮件
		public function delmail(){
			$myid=$_SESSION[C('USER_AUTH_KEY')]['UserId'];
			$mail = M()->execute(' update mail set IsDeleted=1 where Id='.$this->_get('id').' and (Sender = '.$myid.'  or Receiverid = '.$myid.' )');
			if($mail){
				$newnum = $this->newmailnum();
				echo '<script>window.parent.delcallback(true,'.$this->_get('id').','.$newnum.')</script>';
			}else{
				echo '<script>window.parent.delcallback(false,null,null)</script>';
			}
		}
		
		//新邮件数
		public function newmailnum(){
			$mail = M()->query('select count(Id) num from mail where Receiverid='.$_SESSION[C('USER_AUTH_KEY')]['UserId'].' and IsRead=0 and IsDeleted=0');
			return $mail[0]['num'];
		}
		
		//读取邮件
		public function readmail(){
			$new = $this->_get('new');
			$iread = $this->_get('iread');
			if(!$iread){
				$mail = M()->execute(' update mail set IsRead=1 where Id='.$this->_get('id'));
			}
			$mail = M()->query('
				select Id,Title ,Content,Sender ,SenderName,CreateDate 
				from mail
				where Id='.$this->_get('id').' 
			');
			$newmailnum=$this->newmailnum();
			if($newmailnum>0){
				echo '<script>chanmailnul('.$newmailnum.',true);</script>';
			}else{
				echo '<script>chanmailnul('.$newmailnum.',false);</script>';
			}
			if($new){
				echo '<script>changenew('.$this->_get('id').');</script>';
			}
			if($this->_get('mysend')){
				$this->assign('mysend',true);
			}
			$this->assign('mailinfo',$mail[0]);
			$this->display();
		}
		
		//发邮件
		public function smail(){
			$userinfo = $this->getuserinfo($this->_get('id'));
			$this->assign('userdetail',$userinfo);
			$this->assign('uid',$this->_get('id'));
			$this->display();
		}
		
		//用户信息查询
		private function getuserinfo($id){
			return M()->query('select TrueName from users where UserId='.$id);
		}
		
		//批量删除
		public function delsome(){
			$ids=rtrim($this->_post('ids'),',');
			$mail = M()->execute(' update mail set IsDeleted=1 where Id in ('.$ids.')' );
			if($mail){
				echo $this->newmailnum();
				return true;
			}else{
				return false;
			}
		}
		
		//标记已读
		public function disread(){
			$ids=rtrim($this->_post('ids'),',');
			$mail = M()->execute(' update mail set IsRead=1 where Id in ('.$ids.')' );
			if($mail){
				echo $this->newmailnum();
				return true;
			}else{
				return false;
			}
		}
		//标记未读
		public function dnoread(){
			$ids=rtrim($this->_post('ids'),',');
			$mail = M()->execute(' update mail set IsRead=0 where Id in ('.$ids.')' );
			if($mail){
				echo $this->newmailnum();
				return true;
			}else{
				return false;
			}
		}
		
		//发信
		public function mailto(){
			$mail = new MailModel($this->_post('mailto'),$_SESSION[C('USER_AUTH_KEY')]['UserId']);
			$mail->title = htmlspecialchars($this->_post('Title'));
			$mail->content = htmlspecialchars($this->_post('Content'));
			$res=$mail->sendmail();
			if($res){
				echo "<script>window.parent.uploadCallBack(true);</script>";
			}
		}
	}